<template>
  <page title="需求申请表详情" :has-back="true">
    <tips :msg="msg" v-if="tipsShow"></tips>
    <approve-progress v-model="progress"></approve-progress>
    <basic-info-form v-model="form.project"></basic-info-form>
    <acc-needs-form v-model="form.accNeed" :editable="accEdit" :dates="form.dates"></acc-needs-form>
    <food-needs-form v-model="form.foodNeed" :editable="foodEdit"></food-needs-form>
    <classroom-needs-form v-model="form.classroomNeed" :editable="classroomEdit" :dates="form.dates"></classroom-needs-form>
    <el-row justify="center">
      <el-button style="width: 200px" type="primary" @click="save" v-if="edit">
        保存
      </el-button>
      <el-button style="width: 200px" type="primary" @click="back" v-else>
        返回
      </el-button>
    </el-row>
  </page>
</template>

<script>
import page from "@/components/Page";
import ApproveProgress from "@/components/ApproveProgress";
import BasicInfoForm from "@/components/BasicInfoForm";
import AccNeedsForm from "@/components/AccNeedsForm";
import FoodNeedsForm from "@/components/FoodNeedsForm";
import ClassroomNeedsForm from "@/components/ClassroomNeedsForm";
import Tips from "@/components/tips";
import axios from "axios";
import moment from "moment";
export default {
  name: "NeedsFormInfo",
  components: {ClassroomNeedsForm, FoodNeedsForm, AccNeedsForm, BasicInfoForm, ApproveProgress,page,Tips},
  data(){
    return{
      edit:false,
      accEdit:false,
      foodEdit:false,
      classroomEdit:false,
      msg:'审批通过后，请在4月25日前确认需求表，未及时确认需求表会导致需求表自动作废！',
      progress:[{
        result:'',
        needType:''
      }],
      form:{
        project:{
          name: '',
          startDate: '',
          endDate: '',
          people: '',
          part: {},
          user: {},
        },
        accNeed: {
          checkInWay: 0,
          detailList:[],
          detail:'',
        },
        foodNeed:{
          detailList:[],
          detail:"",
        },
        classroomNeed:{
          detailList:[],
          detail:"",
        },
        //传递日期
        dates:[]
      },
      lastDate:''
    }
  },
  methods:{
    save(){
      let type = [];
      this.progress.forEach((approve)=>{
        if(approve.result === false){
          type.push(approve.needType);
        }
      })
      this.form.classroomNeed.detailList = this.form.classroomNeed.detailList.filter((value => {
        return value !== undefined;
      }))
      axios.put(this.$host+"/form",this.form,{
        params:{
          type: type.toString()
        }
      }).then((response)=>{
        if(response.data.code === 0){
          this.$message.success("修改成功");
          this.$router.replace("/index");
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    back(){
      this.$router.back()
    },
    findFormByProjectId(id){
      return axios.get(this.$host+"/form/"+id).then(response=>{
        console.log(response.data.data);
        if(response.data.code === 0){
          this.form = response.data.data;
        }
      })
    },
    findFormByProjectUid(uid){
      return axios.get(this.$host+"/form/uid/"+uid).then(response=>{
        console.log(response.data.data);
        if(response.data.code === 0){
          this.form = response.data.data;
        }
      })
    },
    findApproveByProjectUid(uid){
      return axios.get(this.$host+"/approve",{
        params:{
          uid:uid
        }
      }).then((response)=>{
        if(response.data.code === 0){
          this.progress = response.data.data;
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    initMsg(){
      let updateTime = this.form.project.startDate;
      if(updateTime){
        updateTime = moment(updateTime);
        let lastTime = updateTime
            // .add(7,'d');
        this.msg = '审批通过后，请在'+lastTime.format("M月D日")+'前确认需求表，未及时确认需求表会导致需求表自动作废！'
      }
    }
  },
  computed:{
    tipsShow(){
      return Number(this.form.project.projectStatus) <= 3;
    }
  },
  created() {
    this.edit = this.$router.currentRoute.value.query.edit === "1";
    let id = this.$router.currentRoute.value.query.id;
    let uid = this.$router.currentRoute.value.query.uid;
    if(id !== undefined){
      this.findFormByProjectId(id).then(()=>{
        this.initMsg();
        this.findApproveByProjectUid(this.form.project.uid).then(()=>{
          if(this.edit){
            console.log(this.progress);
            this.progress.forEach((approve)=>{
              if(approve.result === false){
                if(approve.needType === 1)
                  this.accEdit = true;
                if(approve.needType === 2)
                  this.classroomEdit = true;
                if(approve.needType === 3)
                  this.foodEdit = true
              }
            })
          }
        })
      });
    }else if(uid !== undefined){
      this.findFormByProjectUid(uid).then(()=> {
        this.findApproveByProjectUid(this.form.project.uid);
      });
    }
  }
}
</script>

<style scoped>

</style>